package com.huizhonglm.diap.ui.wallet;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.model.wallet.CashInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 我的现金数据适配器
 */
public class MyCashAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<CashInfo> data;

    public MyCashAdapter(LayoutInflater inflater, List<CashInfo> data) {
        this.inflater = inflater;
        this.data = data;
    }

    public void setData(List<CashInfo> data) {
        this.data = data;
    }

    @Override
    public int getCount() {
        return EmptyUtil.isEmpty(data) ? 0 : data.size();
    }

    @Override
    public CashInfo getItem(int position) {
        return EmptyUtil.isEmpty(data) ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.item_my_cash_list, parent, false);
            holder.textViewCashType = (TextView) view.findViewById(R.id.textViewCashType);
            holder.textViewCashValue = (TextView) view.findViewById(R.id.textViewCashValue);
            holder.textViewCashDate = (TextView) view.findViewById(R.id.textViewCashDate);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        CashInfo item = getItem(position);
        String cashValue = item.getCashValue();
        if (item.getCashType().equalsIgnoreCase("5") || item.getCashType().equalsIgnoreCase("6")) {
            cashValue = "-" + cashValue;
            holder.textViewCashValue.setTextColor(0xfff54545);
        }else{
            cashValue = "+" + cashValue;
            holder.textViewCashValue.setTextColor(0xff999999);
        }
        holder.textViewCashType.setText(item.getCashName());
        holder.textViewCashValue.setText(cashValue);
        holder.textViewCashDate.setText(getStrTime(item.getCashDate()));
        return view;
    }

    private class ViewHolder {
        private TextView textViewCashType;
        private TextView textViewCashValue;
        private TextView textViewCashDate;
    }

    public static String getStrTime(String cc_time) {
        String re_StrTime = null;
        //同理也可以转为其它样式的时间格式.例如："yyyy/MM/dd HH:mm"
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time));

        return re_StrTime;
    }
}



